package com.kalix.btzjjj.txt.biz;

import com.kalix.btzjjj.txt.api.biz.IAppManagerBeanService;
import com.kalix.btzjjj.txt.api.dao.IAppManagerBeanDao;
import com.kalix.btzjjj.txt.api.model.InfUtil;
import com.kalix.btzjjj.txt.entities.AppManagerBean;
import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import org.json.JSONArray;

/**
 *@ Author  ZhaoHang
 * @Date  2019/8/28
 **/
public class AppManagerBeanServiceImpl extends GenericBizServiceImpl<IAppManagerBeanDao, AppManagerBean> implements IAppManagerBeanService {
    String txtIp = (String) ConfigUtil.getConfigProp("FREEBOARD_TXT_IP","config.btzjjj.portconf");
    @Override
    public String getValue() {
        //获取从txt文件读出的内容
//        JSONArray jArray = ReadFileUtil.ReadFileUtil();
        String last = "";
        String str = InfUtil.interfaceUtil(txtIp,"");
        String key = str.substring(0, 304);
        String all = str.substring(304, str.length());
        String[] name = key.split(",");
        String[] value = all.split(",");
        String appCount  = null;
        String appAlarmPercent  = null;
        String appAlarmCount = null;
        String longestAlarmAppTime  = null;
        for (int i = 0; i < name.length; i++) {
            if ("appCount".equals(name[i])) {
                appCount = value[i];
            }
            if ("appAlarmPercent".equals(name[i])) {
                appAlarmPercent = value[i];
            }
            if ("appAlarmCount".equals(name[i])) {
                appAlarmCount = value[i];
            }
            if ("longestAlarmAppTime".equals(name[i])) {
                longestAlarmAppTime = value[i];
            }
        }
        if(appAlarmCount.equals("-")){
            last = "{" +
                    "\"day\": \"-\"," +
                    "\"hour\": \"-\"," +
                    "\"minute\": \"-\"," +
                    "\"percent\": \"-\"," +
                    "\"data\": [" +
                    "{" +
                    "\"value\": \""+appAlarmCount+"\"," +
                    "\"name\": \"当前应用系统故障数\"" +
                    "}," +
                    "{" +
                    "\"value\":" ;
            last+=" \"-\"," +
                "\"name\": \"当前应用系统正常数\"" +
                "}" +
                "]" +
                "}";
        }else{
            int second = Integer.parseInt(longestAlarmAppTime);
            int day = second/86400;
            int hour = second%86400/3600;
            int minute = second%86400%3600/60;
            last = "{" +
                    "\"day\": \""+String.valueOf(day)+"\"," +
                    "\"hour\": \""+String.valueOf(hour)+"\"," +
                    "\"minute\": \""+String.valueOf(minute)+"\"," +
                    "\"percent\": \""+appAlarmPercent+"\"," +
                    "\"data\": [" +
                    "{" +
                    "\"value\": \""+appAlarmCount+"\"," +
                    "\"name\": \"当前应用系统故障数\"" +
                    "}," +
                    "{" +
                    "\"value\":" ;
            last+=" \""+String.valueOf(Integer.parseInt(appCount)-Integer.parseInt(appAlarmCount))+"\"," +
                "\"name\": \"当前应用系统正常数\"" +
                "}" +
                "]" +
                "}";
            }
        return last;
    }
}
